<template>
   <el-button type="warning" @click="download">下载</el-button>
</template>

<script setup lang="ts">
import {fileStore} from '@/stores/disk/file';
import {ElMessage} from 'element-plus';
import {downloadFile} from '@/api/disk/file';
const store = fileStore();


// 下载文件
const download = async() => {
    if(store.state.currentFiles.length<0){
        ElMessage.error('请选择文件');
        return;
    }
    //请求接口
    try {
        const res = await downloadFile(store.state.currentFiles)
        const url = window.URL.createObjectURL(new Blob([res]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'download.zip');
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(url);
    } catch (error) {
        console.error('下载文件时出错:', error);
        ElMessage.error('下载文件时出现错误，请稍后再试');
    }
    
}
</script>

<style scoped>

</style>